package mapEditor.obj;

import debug.*;

import mapEditor.map.ObjectModel;
import mapEditor.utils.MapLocation;
import mapEditor.utils.FillStyle;
import mapEditor.utils.StrokeStyle;
import mapEditor.utils.Util;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;


//DOC: http://www.w3.org/TR/SVG11/struct.html#ImageElement
//DOC: http://www.zvon.org/xxl/svgReference/Output/el_line.html
public class ImageObject extends DrawObject
{ private static final String TAG="mapEditor.obj.ImageObject";
//  private static final boolean VERBOSE=true;
  private static final boolean DEBUG=true;
 
  public static final String TYPE="Image";
  private String uri;
  private Image image;

  public ImageObject(ObjectModel objectModel,MapLocation location1,MapLocation location2,String uri) throws IOException,JSONException
  { super(objectModel,null);
//    stroke.setEnabled(true);
    fill.setEnabled(false);
    stroke.setEnabled(false);
	add(location1);
	add(location2);
	this.uri=uri; loadImage();
	this.setClosedPath(true);
  }
  
  public ImageObject(ObjectModel objectModel,JSONObject jsonObject) throws IOException,JSONException
  { super(objectModel,jsonObject.getString("id"));
	if(DEBUG) Log.d(TAG,"decodeSVG: jsonObject="+jsonObject);
    this.uri=jsonObject.getString("href");
    if(DEBUG) Log.d(TAG,"decodeSVG: uri="+uri);
    loadImage();
    this.fill=new FillStyle(jsonObject);
    this.stroke=new StrokeStyle(jsonObject);
    clearPointSet();
    JSONArray p1=jsonObject.getJSONArray("p1");
    JSONArray p2=jsonObject.getJSONArray("p2");
    add(MapLocation.newPixelPoint(objectModel,p1));
    add(MapLocation.newPixelPoint(objectModel,p2));
    setClosedPath(true);
  }
  
  public URL getURL() throws IOException
  { URL url=new URL(uri);
    return(url);
  }

  protected Shape getShape(double dx,double dy)
  { return(getBounds(dx,dy)); }

  private Rectangle2D getBounds(double dx,double dy)
  { MapLocation p0=getMapLocation(0);
    MapLocation p1=getMapLocation(1);
    double x0=p0.getX()+dx;
    double y0=p0.getY()+dy;
    double x1=p1.getX()+dx;
    double y1=p1.getY()+dy;
    double x=Math.min(x0,x1);
    double y=Math.min(y0,y1);
    double width=Math.abs(x1-x0);
    double height=Math.abs(y1-y0);
    Rectangle2D shape=new Rectangle2D.Double(x,y,width,height);
    return(shape);	  
  }

  public JSONObject toJSONObject() throws JSONException
  { JSONObject jsonObject=new JSONObject();
    jsonObject.put("type",TYPE);
    jsonObject.put("id",getIdent());
    MapLocation p1=getMapLocation(0);
    MapLocation p2=getMapLocation(1);
    jsonObject.put("id",getIdent());
    jsonObject.put("p1",p1.encode());
    jsonObject.put("p2",p2.encode());
    jsonObject.put("href",uri);
    jsonObject.put("fill",fill.encode());
    jsonObject.put("stroke",stroke.encode());
	return(jsonObject);
  }
  
  // ******************************************************************
  // *** UTILITY loadImage
  // ******************************************************************
  private void loadImage() throws IOException
  { URL url=new URL(uri);
	Toolkit toolkit=Toolkit.getDefaultToolkit();
	this.image=toolkit.getImage(url);
  }
  
  // *************************************************************************
  // *** METHOD: paint
  // *************************************************************************
  public void paint(Graphics2D g,ImageObserver imageObserver)
  { Rectangle2D bounds=getBounds(0,0);
    g.drawImage(image,(int)bounds.getX(),(int)bounds.getY(),(int)(bounds.getWidth()),(int)(bounds.getHeight()),imageObserver);
  }
  
  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    private JButton fillButton;
    
    public void paint(Graphics g)
    { if(DEBUG) Log.d(TAG,"EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
      fillButton.setBackground(fill.getColor());
  	super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util.getTitleBorder(title));
  	  setPreferredSize(new Dimension(350,120));
      setLayout(new GridLayout(5,2));
      add(new JLabel("       Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      fillButton=addEditFillButton(this,true);
      strokeButton=addEditStrokeButton(this,true);
    }
  }

}
